<?php

namespace app\api\controller;


use app\common\controller\ApiController;
use think\App;
use think\facade\Env;

class Upload extends ApiController
{

    protected $noNeedLogin = ['index'];
    protected $mustLogin = [];

    /**
     * 上传图片
     * @return string
     * @throws \Exception
     */
    public function index()
    {
        $file = request()->file('file');

        if($file){ 
        try {
            $res=validate(['file' => [
              'fileExt' => 'jpg,png,gif,jpeg',
              'fileMime' => 'image/jpeg,image/png,image/gif',
              ]])->check(['file' => $file]);
            
            $res=true;
          } catch (\Throwable $th) {
            $res=false;
          }
        
              if($res){
              $savename = \think\facade\Filesystem::disk('public')->putFile('upload', $file);
                 
              $path = 'http://' . $_SERVER['SERVER_NAME'] .'/'. $savename;
         
              $this->success('上传成功',$path);  
              }else{
                $this->error('error');
              }
        }else{
          $this->error('上传失败'); 
        }
    }
}
